import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input, Tag, Select } from "antd";
// http
import http from '../../../utils/request';

const { Option } = Select;


class UserMarkRecordManage extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "操作", dataIndex: "user", key: "user", width: "200px"},
                {title: "来源", dataIndex: "source", key: "source", width: "100px"},
                {title: "动作", dataIndex: "operation", key: "operation", width: "100px"},                
                {title: "时间", dataIndex: "updated", key: "updated", width: "180px"},
                {title: "详情", dataIndex: "detail", key: "detail",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.detail}</Tag>
                            </div>
                        )
                    }
                },
            ],
            loading: false,
            search_detail: null,
            search_operation: null,
            search_source: null,
        }
        this.formRefAdd = React.createRef();
        this.formRefEdit = React.createRef();
        this.formRefMsg = React.createRef();
    }

    componentDidMount() {
        this.loadData()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'detail__contains': this.state.search_detail,
            'operation__contains': this.state.search_operation,
            'source__contains': this.state.search_source,
        }

        // 获取分组列表
        http.get('/inner/user/mark/record', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 业务线搜索 */
    onFinishSearch = (data) => {
        this.setState({
            search_detail: data.detail ? data.detail : null,
            search_operation: data.operation ? data.operation : null,
            search_source: data.source ? data.source : null,
        })
        this.loadData()
    }

    render(){
        const { loading, dataSource, columns, page, total } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="查询" name="detail">
                        <Input placeholder="查询记录数据信息"  />
                    </Form.Item>

                    <Form.Item label="来源" name="source">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择来源"
                            >
                            <Option key="web">web</Option>
                            <Option key="api">api</Option>
                        </Select>                      
                    </Form.Item>

                    <Form.Item label="动作" name="operation">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择动作"
                            >
                            <Option key="add">add</Option>
                            <Option key="update">update</Option>
                            <Option key="delete">delete</Option>
                        </Select>                      
                    </Form.Item>

                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}></div>

                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>
            </Fragment>
        )
    }
}
export default UserMarkRecordManage;